<?php
/**
 * 飞信机器人--功能插件--留言版类
 * 功能：实现留言版，查看留言功能
 * 数据表：fx_guestbook
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
支持参数(fx_menus表 extra_params字段)：
 action=list&classid=分类id: 查看指定分类留言列表
 action=write&classid=分类id: 留言
*/
/*
SQL语句：
CREATE TABLE `fx_guestbook` (
  `id` mediumint(10) NOT NULL auto_increment,
  `class_id` int(14) default '0',
  `buddy_no` int(14) default NULL,
  `buddy_name` varchar(14) collate utf8_bin default NULL,
  `content` varchar(512) collate utf8_bin default NULL,
  `send_time` int(14) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
*/
class GuestBook extends CommonPlugin
{
	var $UUID='6831cf9c-d114-496f-94ae-213eda24e775';
	
	public function GuestBook(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	private function getPage($class_id)
	{
		$page = $this->session->getValue("GuestBook{$class_id}_page");
		if( $page == 0) $page = 1;
		return $page;
	}
	
	private function setPage($class_id, $page)
	{
		$this->session->set("GuestBook{$class_id}_page", $page);
	}
	
	public function getList($class_id,$count)
	{
		$msg = "<Font Face='楷体' Color='-16744320' Size='10.5'>";
		$extra = "";
		$perPage = 4;
		$pages = 1;
		$currentPage = $this->getPage($class_id);
		if($count>$perPage)
		{
			$pages = ceil($count/$perPage);
			$extra = ",(第{$currentPage}页 共{$pages}页)p-上一页 n-下一页";
			$start = $perPage * ($currentPage-1);
			$sql = "select * from fx_guestbook where class_id=$class_id order by id desc limit $start, $perPage";
		}else 
		{
			$sql = "select * from fx_guestbook where class_id=$class_id  order by id desc";
		}
		$retArr = $this->session->db->fetchQueryAll($sql);

		for($i=0; $i<count($retArr); $i++ )
		{
			$msg.=$retArr[$i]['content']."[".$retArr[$i]['buddy_name'] ."--". date('m-d H:s',$retArr[$i]['send_time'])."]\n";
		}
		
		$msg.="</Font>";
		$msg.= $this->getFooter($extra);
		return $msg;
	}
	
	public function getTips()
	{
		$action = $this->extraParam['action'];
		$class_id=$this->extraParam['classid'];
		if( $action== 'write')
		{	
			return "请回复发表你的留言\n".$this->getFooter("");
		}else if( $action== 'list' )
		{
			$sql = "select count(*) as count from fx_guestbook";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			if( $count == 0 )
			{
				return "还没有留言".$this->getFooter();
			}
			return $this->getList($class_id,$count);
		}
		return "";		
	}

	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		
		$action = $this->extraParam['action'];
		$class_id=$this->extraParam['classid'];
		if( $action== 'list')
		{
			$extra = "";
			$perPage = 4;
			$pages = 1;
			$currentPage = $this->getPage($class_id);
			
			$sql = "select count(*) as count from fx_guestbook where class_id=$class_id";
			$retArr = $this->session->db->fetchQueryAll($sql);
			$count = $retArr[0]['count'];
			
			if($count>$perPage)
			{
				$pages = ceil($count/$perPage);
			}				
			if( $input == 'p' )	//上一页
			{
				if($currentPage>1)
				{
					$currentPage--;
					$this->setPage($class_id, $currentPage);
				}
				return $this->getList($class_id,$count);
			}else if( $input == 'n') //下一页
			{
				if($currentPage<$pages)
				{
					$currentPage++;
					$this->setPage($class_id, $currentPage);
				}
				return $this->getList($class_id,$count);
			}
		}else if( $action== 'write' )
		{
			$buddyNo = $this->session->buddyID;
			$buddyName = $this->session->buddyArray['nickname'];
			if(empty($buddyName))$buddyName = $this->session->buddyArray['localname'];
			if(empty($buddyName))$buddyName = $this->session->buddyArray['sid'];
			if(empty($buddyName))$buddyName = $this->session->buddyArray['mobile'];
			
			$content = mysql_real_escape_string($input);
			$send_time = time(NULL);
			
			$sql = "insert into fx_guestbook(class_id,buddy_no,buddy_name,content,send_time) ";
			$sql.= "values({$class_id},'{$buddyNo}','{$buddyName}','{$content}','{$send_time}')";
			$this->session->db->execute($sql);
			return "您的发言已成功！\n".$this->getFooter('');
		}
		return $msg;
	}
	
}
?>